<div id="header"><p>Your Sensors</p></div>
<form name = "rename" action = "?p=sensormanagement&action=rename" method="post">
<?php
	
	// First, get association array of all active sensors
	$sensors = get_active_sensors($database);

	// Next, get the battery levels of each sensor
	$batteries = get_sensor_battery_levels($database);

	// Renaming a sensor?
	if($_GET["action"] === "rename")
	{
			// Go through each sensor and call the nameSensor procedure
		  foreach($sensors as $id=>$name)
		  {	
		      $new = $_POST["newname$id"];
		      $sensors[$id] = $new;
		      $database->multi_query("CALL nameSensor($id,\"$new\")");
		      if($database->more_results())
		          while($database->next_result());
		  }
	}

	// Create subheader and form for each sensor
	foreach($sensors as $id=>$name)
	{
		$output = '<div id="subheader"><p>';

		// If the sensor is unnamed, simply write 'Sensor #', otherwise, write the name of the sensor
		//  in the subheader
		if($name == "")
		{
				$output = $output.'Sensor '.$id;
		}
		else
		{
				$output = $output.$sensors[$id].' (Sensor '.$id.')';
		}

		// Add battery level icon
		if(isset($batteries[$id]))
		{
			// Returns the image file for the battery level
			$icon = get_icon_of_battery_level($batteries[$id]);
			$output .= "<img src=\"$icon\"/>";
		}	

		echo $output.'</p></div>';

		// Add the textfield for setting the name
		echo '<label for="newname">Name:</label>
					<input type="text" name="newname'.$id.'" value="'.$name.'" />';
	}
?>
<p class="submit"><input type="submit" value="Apply" /></p>
</form>
